use std::io;

fn input(user_msg: &str) -> io::Result<String> {
    use std::io::Write;
    print!("{}", user_msg);
    io::stdout().flush()?;

    let mut buffer = String::new();
    io::stdin().read_line(&mut buffer).expect("stdin error");

    Ok(buffer.trim_end().to_owned())
}
fn input_args() -> (f64, f64, f64) {
    let a = input("a: ")
        .unwrap()
        .parse::<f64>()
        .expect("Cannot parse number");
    let b = input("b: ")
        .unwrap()
        .parse::<f64>()
        .expect("Cannot parse number");
    let c = input("c: ")
        .unwrap()
        .parse::<f64>()
        .expect("Cannot parse number");
    return (a,b,c);
}

fn main() {
    println!("ax² + bx + c = 0");
    println!();
    let (a, b, c) = input_args();
    println!();
    let d = b * b - 4.0 * a * c;
    print!("D = ({} - ({})) = {}", b * b, 4.0 * a * c, d);
    if d < 0.0 {
        println!();
        println!("D < 0");
    } else if d == 0.0 {
        println!();
        let x = (-b) / (2.0 * a);
        println!();
        println!("x = {}", x);
    } else if d > 0.0 {
        let d = d.sqrt();
        println!(" = {}²", d);
        let x1 = ((-b) + d) / (2.0 * a);
        let x2 = ((-b) - d) / (2.0 * a);
        println!();
        println!("x1 = {}", x1);
        println!("x2 = {}", x2);
    }
}
